//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    musicName: ["天空之城", "雨根"],
    musicUrl: ["https://ws-qn-echo-cp-cdn.app-echo.com/c2_96k/57bb6b5334db4b2dadb3cfa887e606db38f3fe97b8165791058e4ffa5b67f32374c83158.mp3?1481446882",
"https://al-qn-echo-cp-cdn.app-echo.com/c2_96k/dfa0066be77e0cfc39515c944927fe619ef24a7e202fef159097b6dfaf2acd48516ea010.mp3?1483265593"],
    musicPlay: "https://ws-qn-echo-cp-cdn.app-echo.com/c2_96k/57bb6b5334db4b2dadb3cfa887e606db38f3fe97b8165791058e4ffa5b67f32374c83158.mp3?1481446882"
  },
  // [{ "天空之城": "https://ws-qn-echo-cp-cdn.app-echo.com/c2_96k/57bb6b5334db4b2dadb3cfa887e606db38f3fe97b8165791058e4ffa5b67f32374c83158.mp3?1481446882" },
  //   { "雨根": "https://al-qn-echo-cp-cdn.app-echo.com/c2_96k/dfa0066be77e0cfc39515c944927fe619ef24a7e202fef159097b6dfaf2acd48516ea010.mp3?1483265593" }]

  //事件处理函数
  musicClick: function() {
    this.setData({ musicDetailState: "musicDetailStates"})
  },

  changeMusic(e) {
    this.data.musicPlay = this.data.musicUrl[e.target.id]
  },

  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onReady: function(){
    // const innerAudioContext = wx.createInnerAudioContext();
    // innerAudioContext.autoplay = true;
    // innerAudioContext.src = this.data.musicPlay;
    // innerAudioContext.onPlay(() => {
    //   console.log('开始播放');
    // });

    // // innerAudioContext.pause(() => {
    // //   console.log("暂停")
    // // })

    // innerAudioContext.onError((res) => {
    //   console.log(res.errMsg);
    //   console.log(res.errCode);
    // });
  },

  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
